package site.wuao.simplerxjava.operator;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Action1;

/**
 * 操作符 - 创建
 *
 * @author wuao
 * @date 2017/11/30
 * @github {https://github.com/CoderWuao}
 * @blogs {http://wuao.site/}
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @github -
 * @blogs -
 * @note -
 */
public class CreateOperator {
    /** 标记 */
    private static final String TAG = "CreateOperator";

    /**
     * interval - 间隔
     *
     * @note 间隔任务
     */
    public void interval() {
        Observable
                .interval(3, TimeUnit.SECONDS)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Log.i(TAG, "call: " + aLong);
                    }
                });

        // call: 0
        // call: 1
        // call: 2
        // call: 3
        // call: 4
        // call: 5
        // ...
    }

    /**
     * range - 范围
     *
     * @note 1.替代for循环
     * 2.第一个是起始值，不小于0
     * 3.第二个是序列的个数
     */
    public void range() {
        Observable
                .range(0, 3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 0
        // call: 1
        // call: 2
    }

    /**
     * repeat - 重复
     */
    public void repeat() {
        Observable
                .range(0, 3)
                .repeat(2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 0
        // call: 1
        // call: 2
        // call: 0
        // call: 1
        // call: 2
    }
}
